#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef int (*cmp_fun)(void *v1, void *v2);
int int_cmp(void *v1, void *v2)
{
	return *(int *)v1 - *(int *)v2;
}
void insertion_sort(int *array, int size, int array_len, cmp_fun cf)
{
	int i = 1, j = 0;
	char *tmp = malloc(size);
	for (i = 1; i < array_len; i++) {
		memcpy(tmp, (char *)array+i*size, size);
		j = i - 1;
		while (j >= 0 && cf((char *)array+size*j, (void *)tmp) > 0){
			memcpy((char *)array+size*(j+1), (char *)array + size*j, size);
			j--;
		}
		memcpy((char *)array+size*(j+1), tmp, size);
	}
	free(tmp);
}

void main(void)
{
	int i;
	int a[] = {4,1,5,7,3,2,6};
	for (i = 0; i < 7; i++) 
		printf ("%d ", a[i]);
	printf("\n");
	insertion_sort(a, sizeof(int), sizeof(a)/sizeof(int), int_cmp);
	for (i = 0; i < 7; i++) 
		printf ("%d ", a[i]);
	printf("\n");

}
